package delegates;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * Created by wzp on 2019/2/22 0022.
 * <p>
 * 描述  ：todo
 */

public abstract class BaseDelegates extends SwipeBackFragment {
    @SuppressWarnings("SpellCheckingInspection")
    private Unbinder mUnbinder = null;


    /*布局可以是一个view 可以是一个layoutid 此处 使用object 作为返回对象*/
    public abstract Object setLayout();

    //强制子类实现该方法 完成点击事件
    public abstract void onBindView(@Nullable Bundle savedInstanceState,View rootview);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = null;
        if (setLayout() instanceof Integer){
            rootView = inflater.inflate((Integer) setLayout(),container,false);
        }else if (setLayout() instanceof  View){
            rootView = (View) setLayout();
        }
        if (rootView != null){
            //绑定视图
            mUnbinder = ButterKnife.bind(this,rootView);
            onBindView(savedInstanceState,rootView);
        }
        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnbinder!=null){
            //解除绑定
            mUnbinder.unbind();
        }
    }
}
